package com.hao.statussheet;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.hao.statussheet.bean.MusicBean;
import com.hao.statussheet.handler.ServiceSendActivityHandler;
import com.hao.statussheet.service.MyMusicService;
import com.hao.statussheet.utils.PalyListUtils;
import com.hao.statussheet.utils.PalyStatus;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

    String urls[] = {
            "http://s0.youdaoli.fm/f/graphics/20161117/582d0ca70f0f7.mp3",
            "http://s0.youdaoli.fm/f/graphics/20161117/582d0cd111160.mp3"
//            "http://s0.youdaoli.fm/f/graphics/20161117/582d0cfa8852e.mp3",
//            "http://s0.youdaoli.fm/f/graphics/20161118/582e7886b9b3d.mp3",
//            "http://s0.youdaoli.fm/f/graphics/20161121/5832599ae1046.mp3",
//            "http://s0.youdaoli.fm/f/graphics/20161121/583259da469f1.mp3",
//            "http://s0.youdaoli.fm/f/graphics/20161108/58219a9783446.mp3",
//            "http://s0.youdaoli.fm/f/graphics/20161108/58219ae8d2a99.mp3",
//            "http://s0.youdaoli.fm/f/graphics/20161121/583280a3be531.mp3",
//            "http://s0.youdaoli.fm/f/graphics/20161114/58290de908207.mp3"
    };


    Fragment fragment_test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        fragment_test = getSupportFragmentManager().findFragmentById(R.id.fragment_test);
        List<MusicBean> list = new ArrayList<>();
        for (int i = 0; i < urls.length; i++) {
            MusicBean musicBean = new MusicBean();
            musicBean.setMusicUrl(urls[i]);
            musicBean.setStatus(PalyStatus.CREAT);
            musicBean.setMusicId(i);
            list.add(musicBean);
        }
        PalyListUtils.addMusicBeanList(list);
        ServiceSendActivityHandler.setOnMusicStatusListener(new ServiceSendActivityHandler.OnMusicStatusListener() {
            @Override
            public void onTime(String time) {
                Log.i(TAG,">>onTime>>>>>" + time);
            }

            @Override
            public void onAllTime(String time) {
                Log.i(TAG,">>onAllTime>>>>>" + time);
            }

            @Override
            public void onUpdate(int position) {
                Log.i(TAG,">>onUpdate>>>>>" + position);
            }

            @Override
            public void onStop() {
                Log.i(TAG,">>onStop>>>>>" );
            }
        });



    }


    public void onTestHind(View view) {
        Log.i(TAG, ">>>>>>>>>>>>>>" + view.getId() + ">>>>" + fragment_test.getUserVisibleHint());

        Log.i(TAG, ">>>>>服务是不是在开启状态>>>>" + isMyServiceRunning());

//        if (fragment_test.getUserVisibleHint()) {
//            fragment_test.setUserVisibleHint(false);
//            getSupportFragmentManager().beginTransaction().hide(fragment_test).commit();
//        } else {
//            fragment_test.setUserVisibleHint(true);
//            getSupportFragmentManager().beginTransaction().show(fragment_test).commit();
//        }
    }

    public void onStopService(View view) {
        Intent intent = new Intent(this, MyMusicService.class);
        stopService(intent);
    }

    private boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.hao.statussheet.service.MyMusicService".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(this, MyMusicService.class);
        stopService(intent);
    }
}
